package com.mxdx.edu.controller;

import com.alibaba.fastjson.JSONObject;
import com.mxdx.edu.common.AddImageToTwoDimensionUtil;
import com.mxdx.edu.common.ApiResult;
import com.mxdx.edu.common.CommonUtils;
import com.mxdx.edu.common.log.AppLogger;
import com.mxdx.edu.enums.*;
import com.mxdx.edu.service.ICmsArticleService;
import com.mxdx.edu.service.ICmsCategoryService;
import com.mxdx.edu.service.IRecruitmentPositionService;
import com.mxdx.edu.service.ISalesManInfoService;
import com.mxdx.edu.util.DictUtils;
import com.mxdx.edu.util.ServletUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务员请求
 *
 * @since 2018-02-11
 * @author wangyuan
 *
 */
@Controller
public class SalesManInfoController extends BaseController{

	private AppLogger logger = new AppLogger(SalesManInfoController.class);

	@Resource
	private ISalesManInfoService salesManInfoService;

	@Value("${recommend.peoples.two.dimension.pic}")
	private String recommendPeoplesTwoDimensionPic;

	@Value("${requestHost}")
	private String requestHost;

	@Value("${recommend.company.two.dimension.pic}")
	private String recommendCompanyTwoDimensionPic;

	/**
	 * 业务员登录后显示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/auth/salesManInfo")
    public ModelAndView salesManInfo(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView result;
		String token = sessionUtil.getTokenSession(request);
		Object object = redisUtil.get(token);
		Map<String, Object> studentMap = (Map<String, Object>)object;
		Map<String, Object> findSalesManMap = new HashMap<>();
		findSalesManMap.put("studentId", CommonUtils.getStrValueFromMap(studentMap,"studentId",""));
		Map<String, Object> salesManMap = salesManInfoService.findMapByMap(findSalesManMap);
		String salesManType = CommonUtils.getStrValueFromMap(salesManMap,"salesManType","");
		String recommendationCode = CommonUtils.getStrValueFromMap(salesManMap,"recommendationCode","");
		Map<String, Object> agnetMap = salesManInfoService.findAgentById(salesManMap);
		Map<String, Object> salesMap ;
		String imageUrl;
		if(SalesManTypeEnums.PEOPLES.getCode().equals(salesManType)){
			result = new ModelAndView("h5/recommendPeoplesSalesMan");
			salesMap = salesManInfoService.findRecommendationPeoplesSalesMan(findSalesManMap);
			imageUrl =  AddImageToTwoDimensionUtil.createTwoDimensionPic(String.format("%s&recommendationCode=%s", recommendPeoplesTwoDimensionPic, recommendationCode),"二维码",null,null,125,125);
		}else{
			result = new ModelAndView("h5/recommendCompanySalesMan");
			salesMap = salesManInfoService.findRecommendationCompanySalesMan(findSalesManMap);
			imageUrl =  AddImageToTwoDimensionUtil.createTwoDimensionPic(String.format("%s&recommendationCode=%s", recommendCompanyTwoDimensionPic, recommendationCode),"二维码",null,null,125,125);;
		}
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		result.addObject("imageUrl",  encoder.encode(Base64.decodeBase64(imageUrl)));
		result.addObject("salesManMap",salesManMap);
		result.addObject("agnetMap",agnetMap);
		result.addObject("salesMap",salesMap);
        return result;
    }

    //根据路径和推荐码生成二维码
	/**
	 * 二维码生成
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/public/salesManInfo/twoDimensionalCode/create")
	public void twoDimensionalCodeCreate(HttpServletRequest request, HttpServletResponse response) {
		String twoDimensionPicUrl = request.getParameter("twoDimensionPicUrl");
		String recommendationCode = request.getParameter("recommendationCode");
		String imgStr = AddImageToTwoDimensionUtil.createTwoDimensionPic(String.format("%s&recommendationCode=%s", twoDimensionPicUrl, recommendationCode),"二维码",null,null,125,125);
		byte[] b = Base64.decodeBase64(imgStr);
		for (int i=0; i<b.length; i++) {
			if (b[i] < 0) {
				b[i] += 256;
			}
		}
		try {
			OutputStream os = response.getOutputStream();
			os.write(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}




	/**
	 * 业务员推荐企业列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/auth/salesManInfo/recommendCompanyDetail")
	public ModelAndView recommendCompanyDetail(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("h5/recommendCompanyDetail");
		String currentPage = ServletUtil.getParameterStr(request, "currentPage", "1");
		String refereesCode = ServletUtil.getParameterStr(request, "refereesCode", "1");
        String companyName = ServletUtil.getParameterStr(request, "companyName", "");
        String status = ServletUtil.getParameterStr(request, "status", "");
		String token = sessionUtil.getTokenSession(request);
		Object object = redisUtil.get(token);
		Map<String, Object> studentMap = (Map<String, Object>)object;
		Map<String,Object> param = new HashMap<>();
		param.put("begin", (Integer.parseInt(currentPage) - 1) * pageSize);
		param.put("endNum", pageSize);
		param.put("limit", "limit");
        param.put("companyName", companyName);
        param.put("status", status);
        param.put("refereesCode", refereesCode);
		List<Map<String, Object>> recommendCompanyList = salesManInfoService.findRecommendationCompanyList(param);
		result.addObject("recommendCompanyList",recommendCompanyList);
		result.addObject("status",status);
		result.addObject("refereesCode",refereesCode);
		return result;
	}


	/**
	 * 业务员推荐企业列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/auth/salesManInfo/moreRecommendCompanyDetail")
	@ResponseBody
	public ApiResult<String> moreRecommendCompanyDetail(HttpServletRequest request, HttpServletResponse response) {
		ApiResult<String> apiResult = new ApiResult<>();
		String currentPage = ServletUtil.getParameterStr(request, "currentPage", "1");
		String companyName = ServletUtil.getParameterStr(request, "companyName", "");
		String refereesCode = ServletUtil.getParameterStr(request, "refereesCode", "");
		String status = ServletUtil.getParameterStr(request, "status", "");
		Map<String,Object> param = new HashMap<>();
		param.put("begin", (Integer.parseInt(currentPage) - 1) * pageSize);
		param.put("endNum", pageSize);
		param.put("limit", "limit");
		param.put("companyName", companyName);
		param.put("status", status);
		param.put("refereesCode", refereesCode);
		List<Map<String, Object>> recommendCompanyList = salesManInfoService.findRecommendationCompanyList(param);
		apiResult.setResult(JSONObject.toJSONString(recommendCompanyList));
		return apiResult;
	}
}
